/**
 * 类名：HomeworkAction
 * 创建日期： 2014-11-26 下午4:35:04
 * 
 */
package com.erms.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import com.erms.bean.Homework;
import com.erms.bean.Task;
import com.erms.bean.User;
import com.erms.dao.HomeworkDAO;
import com.erms.domain.HomeworkService;
import com.erms.domain.TaskService;
import com.erms.util.StringUtil;
import com.erms.util.TimeUtil;

/**
 * @author hmb
 *         备注：
 * 
 */
public class HomeworkAction extends BaseAction {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 6305492949732455609L;

	private Homework			homework;

	// 文件
	private File				file;
	private String				fileContentType;
	private String				fileFileName;

	private String				newFileName;

	@Resource
	private HomeworkService		homeworkService;
	@Resource
	private TaskService			taskService;
	@Resource
	private HomeworkDAO			homeworkDAO;
	// 返回结果
	private String				jsonResult;
	private JSONObject			result;

	public File getFile() {

		return file;
	}

	public void setFile(File file) {

		this.file = file;
	}

	public String getFileContentType() {

		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {

		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {

		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {

		this.fileFileName = fileFileName;
	}

	public String getNewFileName() {

		return newFileName;
	}

	public void setNewFileName(String newFileName) {

		this.newFileName = newFileName;
	}

	public Homework getHomework() {

		return homework;
	}

	public void setHomework(Homework homework) {

		this.homework = homework;
	}

	public String getJsonResult() {

		return result.toString();
	}

	public void setJsonResult(String jsonResult) {

		this.jsonResult = jsonResult;
	}

	public JSONObject getResult() {

		return result;
	}

	public void setResult(JSONObject result) {

		this.result = result;
	}

	@SuppressWarnings("unchecked")
	public String upload() {

		String userID = getUserNameFromSession();
		String type = getTypeFormSession();
		logger.debug("即将提交作业，提交人身份：user=" + userID + ";type=" + type);

		result = new JSONObject();

		if (StringUtil.isNull(userID) || StringUtil.isNull(type)
				|| !STUDENT.equals(type)) {
			logger.debug("登录过期或身份不合法");
			result.put("error", 2);
			result.put("msg", "登录过期或身份不合法");
			return SUCCESS;
		}

		if (homework == null
				|| (StringUtil.isNull(homework.getContent()) && file == null)) {
			result.put("error", 1);
			result.put("msg", "没有有效数据或作业答案与附件同时为空");
			return SUCCESS;
		}

		Integer id = homework.getTask().getId();
		Task task = taskService.find(id);
		if (task == null) {
			logger.debug("不存在该作业任务，id=" + id);
			result.put("error", 1);
			result.put("msg", "不存在该作业任务，id=" + id);
			return SUCCESS;
		} else if (task.getDeadline() - TimeUtil.getTimeStamp() < 0) {
			result.put("error", 1);
			result.put("msg", "已经超过截止日期，不允许提交");
			return SUCCESS;
		}

		logger.debug("布置的作业是:id=" + id + ";title=" + task.getTitle()
				+ ";content=" + task.getContent());
		List<Homework> tempHomeworks = homeworkDAO
				.findByProperty("task.id", id);
		if (tempHomeworks != null) {
			for (int i = 0, n = tempHomeworks.size(); i < n; i++) {
				logger.debug("找到已提交的作业");
				Homework temp = tempHomeworks.get(i);
				if (userID.equals(temp.getUser().getId())) {
					homeworkService.delete(temp.getId());
					break;
				}
			}
		}
		User tempUser = new User();
		tempUser.setId(userID);
		homework.setUser(tempUser);
		if (homeworkService.save(homework, file, fileFileName, fileContentType,
				newFileName)) {
			Homework homework = homeworkService.getHomework();
			logger.debug("保存作业成功，homework id=" + homework.getId());
			result.put("error", 0);

		} else {
			logger.debug("保存作业失败");
			result.put("error", 1);
			result.put("msg", "保存作业失败");
		}
		return SUCCESS;
	}

	/**
	 * 查看作业
	 * 
	 * @see com.erms.action.BaseAction#execute()
	 * @return
	 * @throws Exception
	 */
	@Override
	public String execute() throws Exception {

		// TODO 自动生成的方法存根
		return super.execute();
	}
}
